package com.softserveinc.interactive_board.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.softserveinc.interactive_board.domain.User;
import com.softserveinc.interactive_board.domain.UserType;
import com.softserveinc.interactive_board.service.InteractiveBoardService;
import com.softserveinc.interactive_board.validators.form.AddUserForm;
import com.softserveinc.interactive_board.validators.form.EditUserForm;
import com.softserveinc.interactive_board.validators.form.RegistrationForm;

@Controller
@RequestMapping("/administration")
public class AdministrationController {

	
	@RequestMapping(method = RequestMethod.GET)
	public String def() {
		return "redirect:/administration/";
	}
	
	
	@RequestMapping("/")
	public String index() {
		return "administration/index";
	}
	
	
	@RequestMapping("/users")
	public String users() {
		return "redirect:/administration/users/";
	}
	
	
	@RequestMapping("/users/")
	public String users(Model model) {
		model.addAttribute("users", interactiveBoardService.getAllUsers());
		return "administration/users/index";
	}

	
	//----------------------------Add-------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/users/add")
	public String usersAdd(Model model) {
//		model.addAttribute("userAttr", new User());
		model.addAttribute("addUserForm", new AddUserForm());
		model.addAttribute("userTypesAttr", this.interactiveBoardService.getRegistratebleUserTypes());
		return "administration/users/add";
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/users/add")
	public String usersAdd(Model model, @Valid final AddUserForm form, final BindingResult result) {
		if (result.hasErrors()) {
			model.addAttribute("userTypesAttr", this.interactiveBoardService.getRegistratebleUserTypes());
			return "administration/users/add";
		}
		User user = new User();
		user.setLogin(form.getLogin());
		user.setPassword(form.getPassword());
		user.setUserType(new UserType(form.getUserTypeId()));
		this.interactiveBoardService.addUser(user);
		return "redirect:/administration/users/";
	}
	
	//----------------------------Edit------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/users/edit")
	public String usersEdit(@RequestParam("user_id") Long userId, Model model) {
		List<User> list = this.interactiveBoardService.getUser(userId);
		if (list.isEmpty())
			return "redirect:/administration/users";;
		model.addAttribute("editUserForm", new EditUserForm(list.get(0)));
		model.addAttribute("userTypesAttr", this.interactiveBoardService.getRegistratebleUserTypes());
		return "administration/users/edit";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/users/edit")
	public String usersEdit(Model model, @Valid final EditUserForm form, final BindingResult result) {
			if (result.hasErrors()) {
				model.addAttribute("userTypesAttr", this.interactiveBoardService.getRegistratebleUserTypes());
				return "administration/users/edit";
			}
			User user = new User();
			user.setId(form.getId());
			user.setLogin(form.getLogin());
			user.setPassword(form.getPassword());
			user.setUserType(new UserType(form.getUserTypeId()));
			this.interactiveBoardService.updateUser(user);
			return "redirect:/administration/users/";
	}
	
	
	//----------------------------Delete-------------------------------------
	@RequestMapping("/users/delete")
	public String usersDelete(@RequestParam("user_id") Long userId) {
		this.interactiveBoardService.deleteUser(userId);
		return "redirect:/administration/users";
	}


	private InteractiveBoardService interactiveBoardService;
	
	@Autowired
	@Qualifier("interactiveBoardServiceImpl")
	public void setInteractiveBoardService(InteractiveBoardService interactiveBoardService) {
		this.interactiveBoardService = interactiveBoardService;
	}
	
}
